package edu.drexel.info636.assignments.controllers;

import java.io.File;

import lombok.Getter;

import org.springframework.stereotype.Component;

/**
 * Handles file commands.
 * 
 * @author Chase Miller
 * 
 */
@Component
public class FileController extends ControllerBase {

    @Getter
    private String filePath;

    /**
     * Prompts the user for the full path to the file. If invalid, notifies and
     * gives a chance for correction.
     * 
     * @return - Returns the full path as a string.
     */
    public String getFilePathFromUser() {
	String pathToReturn = "";

	boolean continuePrompting = true;
	do {
	    System.out.println("Please enter the full path to the data file, including the filename, and press enter.");

	    pathToReturn = this.scan.nextLine();

	    if (isValidPath(pathToReturn)) {
		continuePrompting = false;
	    } else {
		Boolean userResponse = genericBooleanPromptAndResponse("The filepath you entered is not valid.  Would you like to try again (y) or quit (n)?");

		if (userResponse == false) {
		    // The user has elected to quit, so do so here.
		    System.exit(0);
		} else {
		    continuePrompting = true;
		}
	    }
	} while (continuePrompting);

	this.filePath = pathToReturn;

	return pathToReturn;
    }

    /**
     * Prompts the user until a non-existing file path is provided.
     */
    void ensureNewFile() {
	while (new File(this.filePath).exists()) {
	    boolean userSelection = genericBooleanPromptAndResponse("The specified file already exists.  Would you like to enter a different file (y) or quit (n)?");

	    if (userSelection) {
		this.filePath = this.getFilePathFromUser();
	    } else {
		System.exit(0);
	    }
	}
    }

    /**
     * Ensures that the selected file exists. If it doesn't, provides the user
     * the option to specify a new file.
     */
    public void ensureExistingFile() {

	boolean keepLooping = true;

	while (keepLooping) {
	    File fileObject = new File(this.filePath);
	    if (fileObject.exists()) {
		keepLooping = false;
	    } else {
		keepLooping = genericBooleanPromptAndResponse("Cannot read or modify file because it does not exist. Would you like to enter a different file path (y) or quit (n)?");
		if (keepLooping) {
		    this.filePath = this.getFilePathFromUser();
		} else {
		    System.exit(0);
		}
	    }
	}
    }

    /**
     * Returns true if the provided string is a valid path to a file.
     * 
     * @param path
     *            The path as a string.
     * @return - True if the provided string is a valid path. Otherwise, false.
     */
    private static boolean isValidPath(String path) {
	boolean result = false;

	try {
	    File testFile = new File(path);
	    if (testFile.exists() && !testFile.isDirectory()) {
		result = true;
	    } else {
		if (!testFile.isDirectory()) {
		    // Try creating a new file (as it doesn't exist and isn't a
		    // directory).
		    try {
			result = testFile.createNewFile();
		    } catch (Throwable t) {
		    } finally {
			if (testFile.exists()) {
			    testFile.delete();
			}
		    }
		}
	    }
	} catch (Throwable t) {
	}

	return result;
    }
}